import Hero from "../model/hero.model.js";
import Skill from "../model/skill.model.js";
import { Op } from "sequelize";
// 查询所有英雄
export async function getHeroListService(data) {
  const { name, classification, position } = data;

  // 构建动态查询条件
  const whereClause = {};

  if (name) {
    whereClause.name = { [Op.like]: `%${name}%` };
  }

  if (classification) {
    whereClause.classification = classification;
  }

  if (position) {
    whereClause.position = position;
  }
  return await Hero.findAll({
    where: whereClause,
  });
}
// 根据id 查询所有英雄信息
export async function getHeroInfoService(heroId) {
  // 查询出 技能表中的技能信息

  const skills = await Skill.findAll({
    where: {
      heroId,
    },
  });
  const heroInfo = await Hero.findOne({
    where: {
      heroId,
    },
  });
  heroInfo.dataValues.skillList = skills;
  return heroInfo;
}
// 新增英雄
export async function createHeroService(data) {
  //
  const res = await Hero.create(data);
  return res;
}
// 修改英雄 根据英雄id heroId
export async function updateHeroService(heroId, data) {
  const res = await Hero.update(data, {
    where: {
      heroId,
    },
  });
  return res[0] > 0 ? true : false;
}
// 删除英雄
export async function deleteHeroService(heroId) {
  const res = await Hero.destroy({ where: { heroId } });
  return res > 0 ? true : false;
}
